package com.paw.security.jwt.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @author lvzihai
 * @date 2021/7/13
 **/
@Service
public class DefaultUserDetailService implements UserDetailsService {

  @Override
  public UserDetails loadUserByUsername (String username) throws UsernameNotFoundException {
    // 从数据库中获取用户信息， 示例代码

    PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
    String password = passwordEncoder.encode("123456");
    List<GrantedAuthority> authorityList = new ArrayList<>();
    SimpleGrantedAuthority authority = new SimpleGrantedAuthority("admin");
    authorityList.add(authority);
    User user = new User(username,password,authorityList);

    return user;
  }
}
